﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 影楼婚纱摄影系统
{
    public partial class 管理员管理 : Form
    {
        public 管理员管理()
        {
            InitializeComponent();
        }
        private void 管理员管理_Load_1(object sender, EventArgs e)
        {
            QueryAllAdmin();
        }
        private void QueryAllAdmin()
        {
            //数据库连接串
            //string connStr = "Data Source=DESKTOP-P8IR1OF;Initial Catalog=marry;Integrated Security=True";
            string str = System.Configuration.ConfigurationManager.ConnectionStrings["marry"].ConnectionString;
            //创建SqlConnection的实例
            SqlConnection conn = null;
            try
            {
                conn = new SqlConnection(str);
                //打开数据库
                conn.Open();
                string sql = "select * from admin";
                //创建SqlDataAdapter类的对象
                SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
                //创建DataSet类的对象
                DataSet ds = new DataSet();
                //使用SqlDataAdapter对象sda将查新结果填充到DataSet对象ds中
                sda.Fill(ds);
                //设置表格控件的DataSource属性
                dataGridView1.DataSource = ds.Tables[0];
                //设置数据表格上显示的列标题
                dataGridView1.Columns[0].HeaderText = "账号";
                dataGridView1.Columns[1].HeaderText = "密码";
                dataGridView1.Columns[2].HeaderText = "姓名";
                dataGridView1.Columns[3].HeaderText = "性别";
                dataGridView1.Columns[4].HeaderText = "电话号码";
                //设置数据表格为只读
                dataGridView1.ReadOnly = true;
                //不允许添加行
                dataGridView1.AllowUserToAddRows = false;
                //背景为白色
                dataGridView1.BackgroundColor = Color.White;
                //只允许选中单行
                dataGridView1.MultiSelect = false;
                //整行选中
                dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            }
            catch (Exception ex)
            {
                MessageBox.Show("查询错误！" + ex.Message);
            }
            finally
            {
                if (conn != null)
                {
                    //关闭数据库连接
                    conn.Close();
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form myform = new 添加管理员();
            myform.ShowDialog();
            QueryAllAdmin();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //获取DataGridView控件中的值
            //获取管理员账号
            string aId = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            //获取管理员密码
            string aPwd = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
            //获取姓名
            string aName = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
            //获取性别
            string aGender = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
            //获取电话号码
            string aPhoNum = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
            //创建updateForm类的对象，并将课程信息传递给修改界面
            Form updateform = new update_admin(aId, aPwd, aName, aGender, aPhoNum);
            //弹出修改信息窗口
            DialogResult dr = updateform.ShowDialog();
            //判断是否单击确定按钮
            if (dr == DialogResult.OK)
            {
                //调用查询全部课程方法    
                QueryAllAdmin();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //获取DataGridView控件中选中行的编号列的值
            int id = int.Parse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString());
            //数据库连接串
            //string connStr = "Data Source=DESKTOP-P8IR1OF;Initial Catalog=marry;Integrated Security=True";
            string str = System.Configuration.ConfigurationManager.ConnectionStrings["marry"].ConnectionString;
            //创建SqlConnection的实例
            SqlConnection conn = null;
            try
            {
                conn = new SqlConnection(str);
                //打开数据库
                conn.Open();
                //填充占位符
                string sql = string.Format("delete from admin where aId='{0}'", id);
                //创建SqlCommand类的对象
                SqlCommand cmd = new SqlCommand(sql, conn);
                //执行SQL语句
                cmd.ExecuteNonQuery();
                //弹出消息提示删除成功
                MessageBox.Show("删除成功！");
                //调用查询全部的方法，刷新DataGridView控件中的数据
                QueryAllAdmin();
            }
            catch (Exception ex)
            {
                MessageBox.Show("删除失败！" + ex.Message);
            }
            finally
            {
                if (conn != null)
                {
                    //关闭数据库连接
                    conn.Close();
                }
            }
        }


    }
}
